<?php

class Time extends Core {
	public function relative($dt, $precision = 2) {
		$times = array(
			365 * 24 * 60 * 60 => 'ano',
			30 * 24 * 60 * 60 => 'm&ecirc;s',
			7 * 24 * 60 * 60 => 'semana',
			24 * 60 * 60 => 'dia',
			60 * 60 => 'hora',
			60 => 'minuto',
			1 => 'segundo'
		);
		$passed = time() - strtotime($dt);
		if ($passed < 5) {
			$output = '5 segundos atr&aacute;s';
		} else {
			$output = array();
			$exit = 0;
			foreach ($times as $period => $name) {
				if ($exit >= $precision || ($exit > 0 and $period < 60)) {
					break;
				}
				$result = floor($passed / $period);
				if ($period == 30 * 24 * 60 * 60 && $result > 1) {
					$name = 'mese';
				}
				if ($result > 0) {
					$output[] = $result . ' ' . $name . ($result == 1 ? '' : 's');
					$passed -= $result * $period;
					$exit++;
				} elseif ($exit > 0) {
					$exit++;
				}
			}
			$output = implode(' e ', $output) . ' atr&aacute;s';
		}
		return $output;
	}
}

?>